﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using VOL.Core.Extensions;
using VOL.Core.Utilities;
using VOL.Entity.DomainModels.EquipmentManage;
using VOL.Entity.DrugViewing;
using VOL.SmartMecial.Dtos;
using VOL.SmartMecial.Infrastructure;
using VOL.SmartMecial.IServices.DrugViewings;

namespace VOL.SmartMecial.Services.DrugViewings
{
    public class DrugViewingService : IDrugViewingService
    {
        private readonly IBaseRepository<DrugViewingModel> _drugViewingRepository;

        public DrugViewingService(IBaseRepository<DrugViewingModel> drugViewingRepository)
        {
            _drugViewingRepository = drugViewingRepository;
        }
        /// <summary>
        /// 药品入库
        /// </summary>
        /// <param name="drug"></param>
        /// <returns></returns>
        public async Task<string> AddDrugViewing(DrugViewingModel drug)
        {
          
            try
            {
                 var i = await _drugViewingRepository.AddAsync(drug);
                return i.ToString();
            }
            catch (Exception ex)
            {
                return ex.InnerException?.Message ?? ex.Message;
            }
        
        }





        /// <summary>
        /// 药品入库查找药品名称
        /// </summary>
        /// <param name="drugName"></param>
        /// <returns></returns>
        public async Task<WebResponseContent> QueryDrugViewing(string? drugName)
        {
            var drugbiewings =  _drugViewingRepository.GetAll();
            drugbiewings = drugbiewings.WhereIF(!string.IsNullOrEmpty(drugName), c => c.DrugName.Contains(drugName));
            var res = new WebResponseContent();
            res.OK("查询成功", drugbiewings);
            return await Task.FromResult(res);
        }
        /// <summary>
        /// 药品入库--编辑
        /// </summary>
        /// <param name="drug"></param>
        /// <returns></returns>
        public async Task<string> UpdDrugViewing(DrugViewingModel drug)
        {
            try
            {
                var i = await _drugViewingRepository.UpdateAsync(drug);
                return i.ToString();
            }
            catch (Exception ex)
            {
                return ex.InnerException?.Message ?? ex.Message;
            }
        }
    }
}
